#include "picture.h"
//初始化图片链表
struct filetupian * csh()
{
    //申请头节点
    struct filetupian *head = calloc(1,sizeof(struct filetupian));
    if(head == NULL)
    {
    printf("头节点申请失败!\n");
    return NULL;
    }
    head->next = head;
    head->prev = head;
    return head;
}
//申请新节点
struct filetupian *new_node(char filenametp[],enum enumread gs)
{
    struct filetupian *new = calloc(1,sizeof(struct filetupian));
    if(new == NULL)
    {
        printf("新节点申请失败!\n");
        return NULL;
    }
    strcpy(new->filenametp,filenametp);
    new->gs         = gs;
    new->next       = new;
    new->prev       = new;
    return new;
}
//尾插法插入
void endc(struct filetupian *head,struct filetupian *new)
{
    if(head == NULL || new == NULL)
    return;
    head->prev->next = new;
    new->prev  = head->prev;
    head->prev = new;
    new->next  = head;
}
//遍历链表
void bllb(struct filetupian *head,struct lcd_info *lcd)
{
    
    for(struct filetupian *p=head->next;p!=head;p=p->next)
    {
        struct tupian_info *bl = calloc(1,sizeof(struct tupian_info));
        printf("%s\n",p->filenametp);
        tupiansj(p->gs,bl,p->filenametp);
        displaytu(p->gs,lcd,bl);
        free(bl);
        bl = NULL;         
        sleep(1);
    }  
}
//销毁链表
void xh(struct filetupian *head)
{
    //循环销毁释放
    while(1)
    {
    //遍历链表，从最后一个开始销毁
    struct filetupian *p;
    for(p=head;p->next!=head;p=p->next);
    p->next->prev = p->prev;
    p->prev->next = p->next;
    p->next = NULL;
    p->prev = NULL;
    //free(p->filenametp);
    free(p);
    if(head->next == head)
    break;
    }
    //销毁头节点
    head->next = NULL;
    head->prev = NULL;
    free(head);
}
//相册链表操作
void tupianqh(struct lcd_info *lcd,struct filetupian *head)
{
    struct filetupian *p = head;
    while(1)
    {
        enum enumread a = fx();
        
        if(a == left)
        {
            p = p->prev;
            if(p == head)
            {
                p = p->prev;
            }
            struct tupian_info *tupian = calloc(1,sizeof(struct tupian_info));
            tupiansj(p->gs,tupian,p->filenametp);
            displaytu(p->gs,lcd,tupian);
            free(tupian);
            tupian = NULL; 
        }
        else if(a == right)
        {
            p = p->next;
            if(p == head)
            {
                p = p->next;
            }
            struct tupian_info *tupian = calloc(1,sizeof(struct tupian_info));            
            tupiansj(p->gs,tupian,p->filenametp);
            displaytu(p->gs,lcd,tupian);
            free(tupian);
            tupian = NULL;             
        }
        else if(a == up)
        {
            bllb(head,lcd);
        }
        else if(a == down)
        {  
            //销毁链表
            xh(head);                   
            break;
        }

    }
}
